﻿/**************************************************************************************
 * Author : NhiHalt6b
 * Project: OFB
 * Date   : 29 - 03 - 2013
 * Module : BLL layer
 * ***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using System.Data;

namespace OFB.BLL.Repository
{
    public class FbTemplateBLL : IFbTemplateBLL
    {
        /// <summary>
        /// declare context
        /// </summary>
        public FbTemplateContext context; 

        /// <summary>
        /// constructor
        /// </summary>
        public FbTemplateBLL()
        {
            context = new FbTemplateContext(); // khoi tao context
        }
        /// <summary>
        /// List feedback template follow page
        /// </summary>
        /// <param name="startRow"></param>
        /// <param name="maxRow"></param>
        /// <param name="totalRow"></param>
        /// <returns></returns>
        public ICollection<Domain.FeedbackTemplate> ListAllFbTemplate(int startRow, int maxRow, out int totalRow)
        {
            IDataParameter para = context.FbTemplate.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<Domain.FeedbackTemplate> lst  = context.FbTemplate.Init("FbTemplate_ListAll", context.FbTemplate.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                      context.FbTemplate.BuildParameter("maximumRows", DbType.Int32, maxRow), para).MapCollection<Domain.FeedbackTemplate>().GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }
        /// <summary>
        /// List question of feedback template
        /// </summary>
        /// <param name="fbTemplateId"></param>
        /// <returns></returns>
        public ICollection<Domain.Question> ListQuestion(int fbTemplateId)
        {
            return context.FbQuestion.Init("FbTemplate_Detail", context.FbQuestion.BuildParameter("FbTemplateId", DbType.Int32, fbTemplateId)).MapCollection<Domain.Question>().GetCollection();
        }
        /// <summary>
        /// List all Criterion
        /// </summary>
        /// <returns></returns>
        public ICollection<Domain.Criteria> ListAllCriterion()
        {
            return context.FbCriteria.Init("FbTemplate_Criterion", null).MapCollection<Domain.Criteria>().GetCollection();
        }
        /// <summary>
        /// Disable a feedback template
        /// </summary>
        /// <param name="FbTemplateId"></param>
        /// <returns></returns>
        public int DisableFbTemplate(int FbTemplateId)
        {

            IDataParameter deleteRowEffectedParam = context.FbTemplate.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_Disable", context.FbTemplate.BuildParameter("FbTemplateId", DbType.Int32, FbTemplateId),
                                                       deleteRowEffectedParam);

            return (int)deleteRowEffectedParam.Value;
        }
        /// <summary>
        /// Check a feedback template is in use
        /// </summary>
        /// <param name="fbTemplateId"></param>
        /// <returns></returns>
        public bool InUse(int fbTemplateId)
        {
            IDataParameter inUseParam = context.FbTemplate.BuildParameter("InUse", DbType.Boolean, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_InUse", context.FbTemplate.BuildParameter("FbTemplateId", DbType.Int32, fbTemplateId),
                                                       inUseParam);

            return (int)inUseParam.Value == 1;
        }
        /// <summary>
        /// Check a feedback template is in use
        /// </summary>
        /// <param name="fbTemplateId"></param>
        /// <returns></returns>
        public bool Existed(string fbName)
        {
            IDataParameter inUseParam = context.FbTemplate.BuildParameter("Existed", DbType.Boolean, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_Existed", context.FbTemplate.BuildParameter("FbName", DbType.String, fbName),
                                                       inUseParam);

            return (int)inUseParam.Value == 1;
        }
        /// <summary>
        /// Create new feedback template
        /// </summary>
        /// <param name="fbTemplateName"></param>
        /// <param name="numOfQuestion"></param>
        /// <returns></returns>
        public int CreateFbTemplate(string fbTemplateName, int numOfQuestion)
        {
            IDataParameter fbTemplateIdParam = context.FbTemplate.BuildParameter("FbTemplateId", DbType.Int32, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_Insert", context.FbTemplate.BuildParameter("FbName", DbType.String, fbTemplateName),
                context.FbTemplate.BuildParameter("NumOfQuestion", DbType.Int32, numOfQuestion),
                                                          fbTemplateIdParam);

            return (int)fbTemplateIdParam.Value;
        }
        /// <summary>
        /// Create new Question
        /// </summary>
        /// <param name="content"></param>
        /// <param name="criteriaId"></param>
        /// <returns></returns>
        public int CreateQuestion(string content, int criteriaId)
        {
            IDataParameter questionIdParam = context.FbTemplate.BuildParameter("QuestionId", DbType.Int32, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_QuestionInsert", context.FbTemplate.BuildParameter("Content", DbType.String, content),
                context.FbTemplate.BuildParameter("CriteriaId", DbType.Int32, criteriaId),
                                                          questionIdParam);
            return (int)questionIdParam.Value;
        }
        /// <summary>
        /// Save Question to Feedback template
        /// </summary>
        /// <param name="fbTemplateId"></param>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public int CreateDetailFbTemplate(int fbTemplateId, int questionId)
        {
            IDataParameter RowEffectedParam = context.FbTemplate.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_DetailInsert", context.FbTemplate.BuildParameter("FbTemplateId", DbType.Int32, fbTemplateId),
                context.FbTemplate.BuildParameter("QuestionId", DbType.Int32, questionId),
                                                       RowEffectedParam);

            return (int)RowEffectedParam.Value;
        }
        /// <summary>
        /// update question of feedback template
        /// </summary>
        /// <param name="quedtionId"></param>
        /// <param name="content"></param>
        /// <param name="criteriaId"></param>
        /// <returns></returns>
        public int UpdateFbTemplate(int quedtionId, string content, int criteriaId)
        {

            IDataParameter RowEffectedParam = context.FbTemplate.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.FbTemplate.Execute("FbTemplate_QuestionUpdate", context.FbTemplate.BuildParameter("QuestionId", DbType.Int32, quedtionId),
                context.FbTemplate.BuildParameter("Content", DbType.String, content),
                context.FbTemplate.BuildParameter("CriteriaId", DbType.Int32, criteriaId),
                                                       RowEffectedParam);

            return (int)RowEffectedParam.Value;
        }
    }
}
